package com.chb.system.service.impl;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chb.system.mapper.DeptMapper;
import com.chb.system.mapper.UserMapper;
import com.chb.system.service.DeptService;
import com.chb.system.service.UserService;
import com.chb.system.vo.DeptVo;
import com.hos.entity.sys.Dept;
import com.hos.entity.sys.User;
import com.hos.utils.WebUtil;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author: 陈海彬
 * @date: Created in 2023/12/8 17:23
 * @description:
 * @modified By:
 * @version:
 */
@Service
public class DeptServiceImpl implements DeptService {

    @Resource
    private DeptMapper deptMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Resource
    private UserService userService;

    @Override
    public List<Dept> getDeptList() {
        LambdaQueryWrapper<Dept> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dept::getStatus, 0);
        wrapper.eq(Dept::getDelFlag, 0);
        return deptMapper.selectList(wrapper);
    }

    @Override
    public Dept selectDeptByDeptId(Long deptId) {
        LambdaQueryWrapper<Dept> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dept::getDeptId, deptId);
        return deptMapper.selectOne(wrapper);
    }

    @Override
    public int updateRegNumberByDeptid(Long deptId) {
        LambdaUpdateWrapper<Dept> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Dept::getDeptId, deptId);
        wrapper.setSql("reg_number = reg_number + 1");
        return deptMapper.update(wrapper);
    }

    @Override
    public Page<Dept> findDeptByCond(DeptVo deptVo) {
        Page<Dept> page = new Page<>(deptVo.getPageNum(), deptVo.getPageSize());
        LambdaQueryWrapper<Dept> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasText(deptVo.getDeptName())) {
            wrapper.like(Dept::getDeptName, deptVo.getDeptName());
        }

        if (Objects.nonNull(deptVo.getDateRange())) {
            wrapper.between(Dept::getCreateTime, deptVo.getDateRange()[0], deptVo.getDateRange()[1]);
        }
        if (StringUtils.hasText(deptVo.getStatus())) {
            wrapper.eq(Dept::getStatus, deptVo.getStatus());
        }
        wrapper.orderByAsc(Dept::getOrderNum);
        return deptMapper.selectPage(page, wrapper);
    }

    @Override
    public Integer addDept(Dept dept) {
        // 添加一个当前的时间
        dept.setCreateTime(new Date());
        dept.setCreateBy(getUserByToken().getUserName());
        return deptMapper.insert(dept);
    }

    @Override
    public Integer updateDept(Dept dept) {
        dept.setUpdateBy(getUserByToken().getUserName());
        return deptMapper.updateById(dept);
    }

    @Override
    public Dept getDeptByDeptId(Long deptId) {
        return deptMapper.selectById(deptId);
    }

    @Override
    public Integer deleteDeptByDeptIds(List<Long> ids) {
        boolean userByDeptIds = userService.getUserByDeptIds(ids);
        if (userByDeptIds) {
            return 0;
        }
        return deptMapper.deleteBatchIds(ids);
    }

    /**
     * @return com.hos.entity.sys.User
     * @create by: 陈海彬
     * @description: 获取请求用户的信息
     * @create time: 2023/12/12 10:39
     */
    private User getUserByToken() {
        String token = WebUtil.getToken();
        String phone = redisTemplate.opsForValue().get("user:" + token);
        return userService.findUserByPhone(phone);
    }

}